/**
 * 
 */
package com.sduept.nwld.dataserver.controller.equipment.assessment.excel;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.sduept.bigdata.document.entity.core.RecordImage;
import com.sduept.bigdata.document.services.RecordImageService;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionExpert;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionItem;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionQuestionEquipment;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionStation;
import com.sduept.bigdata.equipment.assessment.service.InspectionExpertService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 检查站点导出excel的模型，此模型用于待办任务中的excel导入导出，使用的是easyexcel
 * @author lxj
 * @Date 2020年1月11日
 */
@Data
public class ExportInspectionItemDTO {

	/**
	 * 序号
	 */
	@ExcelProperty(value = "序号", index = 0)
	private int no;
	
	/**
	 * 检查项目
	 */
	@ColumnWidth(20)
	@ExcelProperty(value = "检查项目", index = 1)
	private String inspectionItem;
	/**
	 * 检查要求
	 */
	@ColumnWidth(30)
	@ExcelProperty(value = "检查要求", index = 2)
	private String inspectionRequirement;
	/**
	 * 评分标准号
	 */
	@ExcelProperty(value = "对应表单规范序号", index = 3)
	private String inspectionCriterionNo;
	/**
	 * 评分标准
	 */
	@ColumnWidth(60)
	@ExcelProperty(value = "评分标准", index = 4)
	private String inspectionCriterion;
	
	/**
	 * 接受检查单位分组（被检分子公司）
	 */
	@ExcelProperty(value = "被检分子公司", index = 5)
	private String acceptScope;
	
	/**
	 * 接受检查单位（被检单位）
	 */
	@ExcelProperty(value = "被检单位", index = 6)
	private String acceptUnit;
	
	/**
	 * 评价的变电站
	 */
	@ExcelProperty(value = "被检变电站", index = 7)
	private String stationName;
	/**
	 * 评价的变电站的类型
	 */
	@ExcelProperty(value = "变电站类型", index = 8)
	private String stationType;
	
	
	/**
	 * 评价的变电站的电压等级
	 */
	@ExcelProperty(value = "电压等级", index = 9)
	private String voltageLevel;
	
	/**
	 * 问题间隔
	 */
	@ColumnWidth(40)
	@ExcelProperty(value = "问题间隔", index = 10)
	private String equipmentBay;
	
	/**
	 * 隐患描述
	 */
	@ColumnWidth(40)
	@ExcelProperty(value = "隐患描述", index = 11)
	private String problemDescription;
	/**
	 * 风险等级 例： 一级 、二级、三级、四级
	 */
	@ExcelProperty(value = "问题分级", index = 12)
	private String riskLevel;
	/**
	 *  问题数量
	 */
	@ExcelProperty(value = "问题数量", index = 13)
	private String questionAmount;
	/**
	 * 整改建议
	 */
	@ColumnWidth(40)
	@ExcelProperty(value = "整改建议", index = 14)
	private String recommendation;
	/**
	 * 备注信息
	 */
	@ExcelProperty(value = "备注", index = 15)
	private String remarks;
	
	/**
	 * 评价专家的姓名
	 */
	@ExcelProperty(value = "检查人员", index = 16)
	private String inspectionExpert;
	
	/**
	 * 评价专家的电话
	 */
	@ExcelProperty(value = "联系方式", index = 17)
	private String inspectionExpertPhone;
	
	/**
	 * 图片 
	 */
	@ColumnWidth(40)
	@ExcelProperty(value = "图片名称", index = 18)
	private String imageUrl;
	
	@ExcelIgnore
	private Set<RecordImage> images;
	
	
	public static ExportInspectionItemDTO convert(RecordImageService imageService, InspectionExpertService expertService, InspectionStation station, InspectionItem item, int no) {
		if (station == null || item == null)
            throw new NullPointerException();
		ExportInspectionItemDTO dto = new ExportInspectionItemDTO();
		dto.setAcceptScope(station.getAcceptScope());
		dto.setAcceptUnit(station.getAcceptUnit());

		Set<RecordImage> allImages = new HashSet<RecordImage>();
		// 问题间隔
		StringBuilder equipmentBays = new StringBuilder();
		// 问题间隔数量
		StringBuilder equipmentCounts = new StringBuilder();
		// 隐患描述
		StringBuilder problemDescription = new StringBuilder();
		// 整改建议
		StringBuilder equipmentDescription = new StringBuilder();
		//问题图片
		StringBuilder imageUrls = new StringBuilder();
		
		if(item.getQuestionEquipments() != null) {
			for (InspectionQuestionEquipment qe : item.getQuestionEquipments()) {
				equipmentBays.append("[");
				equipmentBays.append(qe.getEquipmentName());
				equipmentBays.append("]");
				equipmentCounts.append(",");
				equipmentCounts.append(qe.getQuestionAmount());
				problemDescription.append("[");
				if(StringUtils.isNotBlank(qe.getHiddenDangerDesc())) {
					problemDescription.append(qe.getHiddenDangerDesc());
				}
				problemDescription.append("]");
				equipmentDescription.append("[");
				if(StringUtils.isNotBlank(qe.getRecommendation())) {
					equipmentDescription.append(qe.getRecommendation());
				}
				equipmentDescription.append("]");
				try {
					List<RecordImage> tmps = imageService.findRecordImageByOid(qe.getId());
					if(null != tmps && tmps.size()>0) {
						allImages.addAll(tmps);
						imageUrls.append("[");
						for (RecordImage image : tmps) {
							imageUrls.append(",");
							imageUrls.append(image.getPath());
						}
						imageUrls.append("]");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		dto.setImages(allImages);
		dto.setImageUrl(imageUrls.toString().replaceAll("\\[,", "\\["));
		dto.setEquipmentBay(equipmentBays.toString().replaceFirst(",", ""));
		dto.setQuestionAmount(equipmentCounts.toString().replaceFirst(",", ""));
		dto.setProblemDescription(problemDescription.toString().replaceFirst(",", ""));
		dto.setRecommendation(equipmentDescription.toString().replaceFirst(",", ""));
		// TODO 应该为问题设备的备注？？？
		dto.setRemarks(item.getRemaker());
		dto.setInspectionCriterion(item.getContent());
		dto.setInspectionCriterionNo(item.getNodeNo());
		InspectionItem inspectionrequirement = item.getParent();
		InspectionItem inspectionitem = inspectionrequirement.getParent(); 
		
		dto.setInspectionItem(inspectionitem.getNodeNo()+" "+inspectionitem.getContent());
		
		dto.setInspectionExpert(item.getActualAssessmentExpertName());
		String expertId = item.getActualAssessmentExpertId();
		if(StringUtils.isNotBlank(expertId)) {
			try {
				InspectionExpert expert = expertService.findById(expertId);
				if(null != expert) {
					dto.setInspectionExpertPhone(expert.getTelephone());
				}
			} catch (Exception e) {

				e.printStackTrace();
			}
		}
		if(StringUtils.isBlank(dto.getInspectionExpert())) {
			dto.setInspectionExpert(station.getInspectionExpert());
			dto.setInspectionExpertPhone(station.getInspectionExpertPhone());
		}
		dto.setInspectionRequirement(inspectionrequirement.getNodeNo()+" "+inspectionrequirement.getContent());
		dto.setNo(no);
		// 扣分标准为0的时候，风险等级作为建议级
		if(0 == item.getDeductionCriteria()) {
			dto.setRiskLevel("建议级");
		} else {
			dto.setRiskLevel(item.getRealRiskLevel()==null ? inspectionrequirement.getRiskLevel() : item.getRiskLevel());
		}
		dto.setStationName(station.getStationName());
		if(null != station.getStationType()) {
			dto.setStationType(station.getStationType().getLabel());
		}
		if(null != station.getVoltagelevel()) {
			dto.setVoltageLevel(station.getVoltagelevel().intValue()+"kV");
		}
		return dto;
	}
}
